<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>我的博客列表</title>
    <link rel="stylesheet" href="css/common.css" />
    <link rel="stylesheet" href="css/list.css" />
    <!-- 引入jQuery -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <link rel="icon" href="img/logo1.png" type="image/x-icon" />
  </head>
  <body>
    <div class="navigation">
      <!-- 头像 -->
      <img src="img/logo1.png" />
      <div class="title">我的博客系统</div>

      <div class="space"></div>

      <a href="blog_lists.html">主页</a>
      <a href="blog_add.html">写博客</a>
      <a href="/user/logout">退出登录</a>
    </div>

    <!-- 页面主体 -->
    <div class="container">
      <div class="container-left">
        <div class="card">
          <img src="img/avatar.png" />
          <h3></h3>
          <a target="_blank" href="https://gitee.com/carefree-state"
            >代码仓库链接</a
          >
          <div class="counter">
            <span class="h">文章</span>
            <span class="h">分类</span>
          </div>

          <div class="counter">
            <span id="count"></span>
            <span>1</span>
          </div>
        </div>
      </div>

      <div class="container-right">
        <div class="article">
          <!-- <div class="blog">
            <div class="title">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</div>
            <div class="date">xxx / 阅读量：xxx</div>
            <div class="content">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...</div>
            <div class="thing">
              <a href="#">查看正文</a>
              <a href="#">修改文章</a>
              <div
                id="del"
                style="background-color: rgba(255, 0, 0, 0.6)"
                onclick="del()"
              >
                删除文章
              </div>
            </div> -->
          <!-- </div> -->
        </div>
      </div>
    </div>
    <script>
      jQuery.ajax({
        type: "get",
        url: "/art/get_mylist",
        success: function (body) {
          if (body.code == 302) {
            alert("抱歉，请先登录！");
            location.href = body.msg;
            return false;
          }
          if (body.code == 200) {
            // 1. 改变左侧窗口
            jQuery(".card img").attr("src", body.data.user.photo);
            jQuery(".card h3").text(body.data.user.name);
            if (body.data.user.git.trim() != "") {
              jQuery(".card a").attr("href", body.data.user.git);
            }
            jQuery("#count").text(body.data.list.length);
            // 2. 显示文章，构造博客html元素
            if (body.data.list.length == 0) {
              jQuery(".article").append(
                jQuery('<h1 style="color: slateblue">暂无文章</h1>')
              );
              return 0;
            }
            for (var blog of body.data.list) {
              var art =
                '<div class="blog"><div class="title">' + blog.title + "</div>";
              art +=
                '<div class="date">' +
                blog.createtime +
                " 阅读量：" +
                blog.rcount +
                "</div>";
              art += '<div class="content">' + blog.summary + "</div>";
              art += '<div class="thing">';
              art +=
                '<a href="blog_detail.html?aid=' + blog.id + '">查看正文</a>';
              art +=
                '<a href="myblog_update.html?aid=' + blog.id + '">修改文章</a>';
              art +=
                '<div id="del" style="background-color: rgba(255, 0, 0, 0.6)" onclick="del(' +
                blog.id +
                ')">删除文章</div>';
              art += "</div></div>";
              jQuery(".article").append(jQuery(art));
            }
          }
        },
      });
      function del(aid) {
        // -1. 确认吗
        if (!confirm("确认删除吗？")) {
          return false;
        }
        // 0. 参数校验
        if (parseInt(aid) == NaN || aid <= 0) {
          return false;
        }
        jQuery.ajax({
          method: "post",
          url: "/art/delete",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify({
            id: aid,
          }),
          success: function (body) {
            if (body.code == 302) {
              alert("抱歉，请先登录！");
              location.href = body.msg;
              return false;
            }
            if (body.code == 200 && body.data == 1) {
              location.href = location.href;
            } else {
              alert("删除失败！\n");
            }
          },
        });
      }
    </script>
  </body>
</html>
